package com.test.ml.filter;

import com.test.ml.utils.ShiroUtil;
import com.test.ml.yqs.enums.RequestStatusEnum;
import com.test.ml.yqs.utills.ResultJson;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.AuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 马亮
 * @version V1.0
 * @Description: TODO
 */
public class LoginFilter extends AuthenticationFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        Subject subject = getSubject(servletRequest, servletResponse);
//        String path = request.getServletPath();
//        System.out.println("path = " + path);
//        if (path.equals("/RPCAFA2A208FA648EA27C1EC30CADFC8B3D")) {
//            return true;
//        }
//        if (subject.getPrincipals() != null) {
//            return true;
//        }
        return super.isAccessAllowed(servletRequest,servletResponse,o);
//        return false;
    }

    /**
     * 会话超时或权限校验未通过的，统一返回401，由前端页面弹窗提示
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response)
            throws IOException {

        ShiroUtil.writeResponse((HttpServletResponse) response, ResultJson.buildResult(RequestStatusEnum.NOT_LOGIN, "请先登录", null));
        return false;

    }
}